package org.cloudie.docs.editor.client.content.dialogs;

import org.cloudie.docs.editor.client.commands.generic.VirtualEntryFolderAddCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.widgets.ImageBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;

/**
 * A dialog window containing a new Icon form.
 */
public class NewFolderDialog extends FormDialog {

  protected static NewFolderDialog instance;
  
  public static NewFolderDialog get() {
    if (instance == null) {
      instance = new NewFolderDialog();
    }
    return instance;
  }

  private static final String[] IMAGE_IDS = new String[] {
	"folder_blue",
	"folder_bookmarks",
	"folder_cyan",
	"folder_desktop",
	"folder_development",
	"folder_green",
	"folder_grey",
	"folder_html",
	"folder_image",
	"folder_locked",
	"folder_important",
	"folder_network",
	"folder_orange",
	"folder_red",
	"folder_sound",
	"folder_txt",
	"folder_video",
	"folder_violet",
	"folder_yellow"
  };
  private TextBox folderLabel;
  private ImageBox folderImage;
  
  /**
   * Constructs a New Icon dialog.
   */
  protected NewFolderDialog() {
    super("New Desktop Folder", true);
    addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    buildForm();
  }

  /**
   * Builds the New Icon form.
   */
  @Override
  protected void buildForm() {
	folderLabel = new TextBox();
	folderLabel.setWidth("300px");
	folderImage = new ImageBox("/images/icons/large/filesystems/%1.png", "folder", IMAGE_IDS);
	folderImage.setPixelSize(50, 50);
    submit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          VirtualEntryFolderAddCommand cmd = new VirtualEntryFolderAddCommand(
        	  null, folderLabel.getValue(), "filesystems/" + folderImage.getValue(),
        	  null, 0, 0, -1);
          cmd.setDistribute(true);
          CommandEvent.fire(cmd);
          hide();
      }
    });
    cancel.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
          hide();
        }
    });
    addField(folderLabel, "Label:");
    addField(folderImage, "Image:");
    super.buildForm();
  }
  
  @Override
  public void hide() {
    super.hide();
    resetForm();
  }
  
  @Override
  public void resetForm() {
	folderLabel.setValue("");
	folderImage.setValue(null);
  }
}
